package com.itheima.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.excption.MyException;
import com.itheima.pojo.SmsHomeNewProduct;
import com.itheima.vo.PageResultVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class HomeNewProductService {

    @DubboReference
    private SmsHomeNewProductApi smsHomeNewProductApi;

    /**
     * 添加首页新品
     */
    public void create(List<SmsHomeNewProduct> newProductList) {

        boolean flag = smsHomeNewProductApi.create(newProductList);

        if (flag){
            throw new MyException("部分新品已添加,不可重复添加哦!");
        }
    }

    /**
     * 批量删除首页新品
     */
    public void delete(List<Integer> ids) {
        smsHomeNewProductApi.delete(ids);
    }

    /**
     * 批量修改首页新品状态
     */
    public void updateStatus(Integer recommendStatus, List<Integer> ids) {
        smsHomeNewProductApi.updateStatus(recommendStatus,ids);
    }

    /**
     * 修改首页新品排序
     */
    public void updateSort(Integer id, Integer sort) {
        smsHomeNewProductApi.updateSort(id,sort);
    }

    /**
     * 分页查询首页新品
     */
    public PageResultVo<SmsHomeNewProduct> list(Integer pageNum, Integer pageSize, String productName, Integer recommendStatus) {
        IPage<SmsHomeNewProduct> page = smsHomeNewProductApi.list(pageNum,pageSize,productName,recommendStatus);
        PageResultVo<SmsHomeNewProduct> pageResultVo = new PageResultVo<>();

        pageResultVo.setList(page.getRecords());
        pageResultVo.setPageNum((int) page.getCurrent());
        pageResultVo.setPageSize((int) page.getSize());
        pageResultVo.setTotal((int) page.getTotal());
        pageResultVo.setTotalPages((int) page.getPages());

        return pageResultVo;
    }
}
